package demo.stan;

import org.junit.Test;

public class DaiKuan {

    public static void main(String[] args) {
//		processInterestRate_CCB(10_0000.0, 12, 0.0032);
//		processInterestRate_CCB(10_0000.0, 24, 0.0032);
//		processInterestRate_CCB(10_0000.0, 36, 0.0032);
        // System.out.println("===================================");
        processInterestRate_BOS(10_0000.0, 24, 0.077);
//		processInterestRate_BOS(10_0000.0, 36, 0.115);
    }

    /**
     * 上海银行装修贷，提前收所有的手续费，按月平摊还本金。
     * <p>
     * 手续费=贷款总额*手续费率
     *
     * @param amount 贷款总额
     * @param months 贷款期数
     * @param lixi   月手续费率
     */
    static void processMonthly_SHYH(double amount, int months, double lixi) {
        System.out.println("------上海银行贷款方案分析------");
        System.out.println("一共贷款：" + amount + "元");
        System.out.println("分" + months + "个月来还款");
        System.out.println("手续费率为：" + lixi);
        double avg = amount / months;
        System.out.println("平均每月还款：本金 " + avg + "元");
        double shouXuFei = lixi * amount;
        System.out.println("手续费为：" + shouXuFei + "，和第一期还款一起还");

        double yue_yuan = 0.0;
        for (int i = 1; i <= months + 1; i++) {
            double tmp = amount - (i - 1) * avg;

            System.out.println("第" + i + "个月持有银行贷款为：" + tmp);
            yue_yuan += tmp;
        }
        System.out.println(months + "月内持有贷款量：" + yue_yuan + " 月*元");

        System.out.println("我的还款：");
        double huanKuan_yue_yuan = shouXuFei * (months - 1);
        System.out.println(months + "月内上交手续费：" + huanKuan_yue_yuan + " 月*元");
        System.out.println(months + "月的资金占用率为：" + huanKuan_yue_yuan / yue_yuan);
        System.out.println("实际" + months / 12 + "年利息年利率为：" + shouXuFei * 12 / yue_yuan);
    }

    /**
     * 建设银行装修贷，按月收手续费。
     * <p>
     * 手续费=贷款总额*月手续费率
     *
     * @param amount 贷款总额
     * @param months 贷款期数
     * @param rate   月手续费率
     */
    static void processInterestRate_CCB(double amount, int months, double rate) {
        System.out.println("------建设银行贷款方案分析------");
        System.out.println("一共贷款：" + amount + "元");
        System.out.println("分" + months + "期来还款（一期为一个月）");
        System.out.println("每期手续费率为：" + rate);
        double needToPayInterestMonthly = rate * amount;// 每月偿还利息
        double interest = needToPayInterestMonthly * months;
        System.out.println("每期手续费为：" + needToPayInterestMonthly + "元," + months + "期共：" + interest + "元");
        double needToPayBalanceMonthly = amount / months;// 每月偿还本金
        double paymentMonthly = needToPayInterestMonthly + needToPayBalanceMonthly;// 每月偿还金额
        System.out.println("每月还款：本金 " + needToPayBalanceMonthly + " 元+每期手续费 " + needToPayInterestMonthly + "元,共计 "
                + paymentMonthly + "元");
        double moneyXmonth = 0.0;
        int actualMonths = 0;
        for (int i = 1; i <= months + 1; i++) {
            double tmp = amount - (i - 1) * paymentMonthly;
            if (tmp < 0) {
                actualMonths = i - 1;
                break;
            }
            System.out.println("第" + i + "个月持有银行贷款为：" + tmp);
            moneyXmonth += tmp;
        }
        double moneyXyear = moneyXmonth / 12;
        System.out.println(actualMonths + "月内持有贷款量：" + moneyXmonth + " 元*月=" + moneyXyear + "元*年");
        System.out.println("实际" + months / 12 + "年贷款的年利率为：" + interest / moneyXyear);
    }

    /**
     * 上海银行装修贷，提前收所有的手续费，按月平摊还本金。
     * <p>
     * 手续费=贷款总额*手续费率
     *
     * @param amount 贷款总额
     * @param months 贷款期数
     * @param rate   月手续费率
     */
    static void processInterestRate_BOS(double amount, int months, double rate) {
        System.out.println("------上海银行贷款方案分析------");
        System.out.println("一共贷款：" + amount + "元");
        System.out.println("分" + months + "个月来还款");
        System.out.println("手续费率为：" + rate);
        double amount_paymentMonthly = amount / months;// 月还款额
        System.out.println("平均每月还款：本金 " + amount_paymentMonthly + "元");
        double interest = rate * amount;// 利息
        System.out.println("手续费为：" + interest + "，和第一期还款一起还");
        int actualMonths = 0;
        double moneyXmonth = 0.0;
        for (int i = 1; i <= months; i++) {
            double tmp = 0.0;
            if (i == 1) {
                tmp = amount;
            } else {
                tmp = amount - interest - (i - 1) * amount_paymentMonthly;
            }
            if (tmp < 0) {
                actualMonths = i - 1;
                break;
            }
            System.out.println("第" + i + "个月持有银行贷款为：" + tmp);
            moneyXmonth += tmp;
        }
        double moneyXyear = moneyXmonth / 12;
        System.out.println(actualMonths + "月内持有贷款量：" + moneyXmonth + " 元*月=" + moneyXyear + "元*年");
        System.out.println("实际" + months / 12 + "年贷款的年利率为：" + interest / moneyXyear);
    }

    @Test
    public void test() {
        double min = 1000.0;
        for (int m = 0; m < 60; m++) {
            double actualRate = processInterestRateNingBo(25_0000, 60, 0.0027, 0.03, m);
            min = Math.min(min, actualRate);
        }
        System.out.println("最小利率是：" + min);

        processInterestRateNingBo(25_0000, 60, 0.007, 0.03, 30);

    }

    /**
     * 宁波银行装修贷，提前收所有的手续费，按月平摊还本金。
     * <p>
     * 手续费=贷款总额*手续费率
     *
     * @param totalLoans     贷款总额
     * @param months         贷款期数
     * @param rate           月手续费率
     * @param prepaymentRate 如果提前还款，剩余款项的手续费
     * @param actualMonths   实际用款月数
     * @return 实际年化利率
     * 实际年化利率 = （产生的总费用/实际使用的金额）/实际月数 * 12
     */
    public static double processInterestRateNingBo(double totalLoans, int months, double rate, double prepaymentRate, int actualMonths) {
        System.out.println("------宁波银行贷款方案分析------");
        System.out.println("一共贷款：" + totalLoans + "元");
        // 每月应还额本金，月还款额
        double amount_paymentMonthly = totalLoans / months;
        System.out.println("分" + months + "个月来还款，每月还款本金：" + amount_paymentMonthly + "元");
        // 月还款手续费
        double feePerM = rate * totalLoans;
        System.out.println("月手续费率为：" + rate + "，月手续费：" + feePerM + " 元");
        System.out.println("如果提前还款，剩余款手续费率为：" + prepaymentRate);

        // 产生的总费用 = 实际月数 * 月手续费 + 贷款余额 * 提前还款手续费率
        // 贷款余额
        double loanBalance = totalLoans - actualMonths * amount_paymentMonthly - actualMonths * feePerM;
        double totalFee = actualMonths * feePerM + loanBalance * prepaymentRate;
        System.out.println(
                String.format("使用%s月，产生费用%s元，剩余待还金额%s元，一次还清费用%s，实际总支出%s元",
                        actualMonths, actualMonths * feePerM, loanBalance, loanBalance * prepaymentRate, totalFee));

        // 实际用款月数
        double moneyXmonth = 0.0;
        for (int i = 1; i <= actualMonths; i++) {
            // 第N月持有银行现金
            double tmp;
            if (i == 1) {
                tmp = totalLoans * i;
            } else {
                tmp = totalLoans - i * rate - (i - 1) * amount_paymentMonthly;
            }
            if (tmp < 0) {
                actualMonths = i - 1;
                break;
            }
            System.out.println("第" + i + "个月持有银行贷款为：" + tmp);
            moneyXmonth += tmp;
        }


        double moneyXyear = moneyXmonth / 12;
        System.out.println(actualMonths + "月内持有贷款量：" + moneyXmonth + " 元*月=" + moneyXyear + "元*年");

        double actualRate = totalFee / moneyXyear;
        System.out.println("实际年化利率:" + actualRate);
        return actualRate;
    }
}
